import type { RouteRecordRaw } from "vue-router";
import { createRouter, createWebHistory } from "vue-router";

declare type Recordable<T = any> = Record<string, T>;

// import.meta.glob() 直接引入所有的模块 Vite 独有的功能
const modules = import.meta.glob("./modules/**/*.ts", { eager: true });
const basicRouteModuleList: RouteRecordRaw[] = [];

// 加入到路由集合中
Object.keys(modules).forEach((key) => {
  // 获取模块中的 default export
  const mod = (modules as Recordable)[key].default || null;

  if (mod != null) {
    const modList = Array.isArray(mod) ? [...mod] : [mod];
    // 将模块中的 AppRouteModule 添加到基础路由
    basicRouteModuleList.push(...modList);
  }
});

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: basicRouteModuleList
});

export default router;
